package bom.controller.organization;


import bom.pojo.organization.Tree;
import bom.service.i.organization.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description: 菜单操作controller
 * @Date: Created in  2018/11/27 16:28
 * @Modified By:
 */
@Controller
@RequestMapping("/tree")
public class TreeController {

    @Autowired
    private TreeService treeService;

    /**
     * 功能描述：跳转到修改菜单节点的页面
     *
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateTreePage")
    public String updateTreePage(Tree entity, Model model) throws Exception {
        entity = treeService.get(entity);
        Tree pTree = null;
        if (entity.getParentId() == 0l) {
            pTree = new Tree();
            pTree.setId(0l);
            pTree.setName("顶层节点");
        } else {
            pTree = treeService.get(new Tree(entity.getParentId()));
        }
        entity.setTree(pTree);
        model.addAttribute("entity", entity);
        return "";
    }

    /**
     * 功能描述：跳转到增加菜单节点的页面
     *
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addTreePage")
    public String addPage(Tree entity, Model model) throws Exception {
        if (entity.getId() == 0) {
            entity = new Tree();
            entity.setId(0l);
            entity.setName("顶层节点");
        } else {
            entity = treeService.get(entity);
        }
        model.addAttribute("entity", entity);
        String page = "";
        return page;
    }

    /**
     * 功能描述：直接加载整个菜单树的数据(且必须要有管理员权限才可以加载该菜单树的数据)
     *
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping(value = "/loadUserTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> loadUserTree() {
        return treeService.loadUserTree();
    }

}
